package com.kitty.game.market.model;

import com.kitty.common.db.BaseEntityGame;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.item.config.ChangeCard;
import com.kitty.game.item.service.ChangeCardService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.task.service.taskHandler.ChangeCardTaskHandler;
import com.kitty.game.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.entity.annotation.*;
import org.nutz.lang.util.NutMap;

import java.util.Date;

@Table("t_goods")
@TableIndexes({@Index(fields={"roleId"}, unique=false),
        @Index(fields={"level"}, unique=false),
        @Index(fields={"type1","type2","type3"}, unique=false)})
public class Goods extends BaseEntityGame<Long> {
    @Id(auto = false)
    private Long id;
    @Column
    private String name;//物品名称
    @Column
    private long roleId;//玩家id
    @Column
    private byte xiangxin;//相性
    @Column
    private String type1;//宠物 高级首饰 装备 法宝 道具
    @Column
    private String type2;//男帽 女帽 普通 玉佩
    @Column
    private String type3;//相性 等级 等

    @Column
    private int price;//价格
    @Column
    private short level;//等级
    @Column
    @ColDefine(width = 1024 * 4)
    private String content;
    @Column
    private Date startTime;
    @Column
    private Date showEndTime;//公示时间
    @Column
    private Date endTime;
    @Column
    @Default("1")
    private int number;
    @Column
    private byte leixin;//类型 背包，宠物，卡套

    @Column
    @ColDefine(customType = "longtext")
    private String petJson;

    private transient Pet pet;

    @Column
    @ColDefine(customType = "longtext")
    private String equipJson;

    private transient RoleEquip roleEquip;

    /**
     * 上一次修改价格的时间戳
     */
    private long lastUpdatePrice;

    public byte getLeixin() {
        return leixin;
    }

    public void setLeixin(byte leixin) {
        this.leixin = leixin;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getRoleId() {
        return roleId;
    }

    public void setRoleId(long roleId) {
        this.roleId = roleId;
    }

    public byte getXiangxin() {
        return xiangxin;
    }

    public void setXiangxin(byte xiangxin) {
        this.xiangxin = xiangxin;
    }

    public String getType1() {
        return type1 != null ? type1 : "";
    }

    public void setType1(String type1) {
        this.type1 = type1;
    }

    public String getType2() {
        return type2 != null ? type2 : "";
    }

    public void setType2(String type2) {
        this.type2 = type2;
    }

    public String getType3() {
        return type3 != null ? type3 : "";
    }

    public void setType3(String type3) {
        this.type3 = type3;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public short getLevel() {
        return level;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setLevel(short level) {
        this.level = level;
    }

    public String getMianbanId() {
        return String.valueOf(id);
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getShowEndTime() {
        return showEndTime;
    }

    public void setShowEndTime(Date showEndTime) {
        this.showEndTime = showEndTime;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public RoleEquip getRoleEquip() {
        return roleEquip;
    }

    public void setRoleEquip(RoleEquip roleEquip) {
        this.roleEquip = roleEquip;
    }

    public long getLastUpdatePrice() {
        return lastUpdatePrice;
    }

    public void setLastUpdatePrice(long lastUpdatePrice) {
        this.lastUpdatePrice = lastUpdatePrice;
    }


    public String getPetJson() {
        return petJson;
    }

    public void setPetJson(String petJson) {
        this.petJson = petJson;
    }

    public String getEquipJson() {
        return equipJson;
    }

    public void setEquipJson(String equipJson) {
        this.equipJson = equipJson;
    }

    public void initSubType(NutMap equipAttr) {
        if (this.getType1().equals("法宝")) {
            this.setType2(roleEquip.getName());
            this.setXiangxin((byte) equipAttr.getInt("fabaoXiangxin"));
        } else if (this.getType1().equals("高级首饰") || this.getType1().equals("低级首饰")) {
            int peidai = roleEquip.queryEquipPos(false);
            if (peidai == 4) {
                this.setType2("项链");
            } else if (peidai == 5) {
                this.setType2("玉佩");
            } else {
                this.setType2("手镯");
            }
            this.setLevel(roleEquip.queryLevel());
        } else if (this.getType1().equals("装备道具")) {
            this.setType2(roleEquip.getName());
        } else if (this.getType1().equals("宠物道具")) {
            this.setType2(roleEquip.getName());
            if ("灵物囊".equalsIgnoreCase(roleEquip.getName())
                    || "拘首环".equalsIgnoreCase(roleEquip.getName())
                    || "困灵砂".equalsIgnoreCase(roleEquip.getName())
                    || "驱力刺".equalsIgnoreCase(roleEquip.getName())
                    || "定鞍石".equalsIgnoreCase(roleEquip.getName())
                    || "控心玉".equalsIgnoreCase(roleEquip.getName())) {
                this.setType2("精怪灵物");
                this.setType3(roleEquip.getName());
            }
        } else if (this.getType1().equals("妖石")) {
            this.setType2(roleEquip.getName());
        } else if (this.getType1().equals("其他道具")) {
            this.setType2(roleEquip.getName());
        } else if (this.getType1().equals("超级黑水晶")) {
            int pos1 = roleEquip.queryEquipPos(true);
            if (pos1 == 1) {
                this.setType2("武器");
            } else if (pos1 == 2) {
                this.setType2("帽子");
            } else if (pos1 == 3) {
                this.setType2("衣服");
            } else {
                this.setType2("鞋子");
            }
        } else if (this.getType1().equals("变身卡")) {
            ChangeCard changeCard = SpringUtils.getBean(ChangeCardService.class).getCardByName(this.getName());
            if (changeCard.getType() == 1) {
                this.setType2("怪物变身卡");
            } else if (changeCard.getType() == 2) {
                this.setType2("变异变身卡");
            } else if (changeCard.getType() == 3) {
                this.setType2("BOSS变身卡");
            } else if (changeCard.getType() == 4) {
                this.setType2("神兽变身卡");
            }
            this.setType3(SpringUtils.getBean(ChangeCardTaskHandler.class).getPolarString(changeCard.getPolar()) + "相性");
        }

        // 修正召唤令。服务端属于其他道具，客户端属于宠物道具
        if (getType2().contains("召唤令")) {
            setType1("宠物道具");
        }

        // 修正天书秘笈。服务端属于宠物道具，客户端属于天技秘笈
        if (getType2().contains("秘笈")) {
            setType1("天技秘笈");
        }
    }

    /**
     * init hook
     */
    public void doAfterInit() {
        if (StringUtils.isNotEmpty(petJson)) {
            pet = JsonUtils.string2Object(petJson, Pet.class);
        }
        if (StringUtils.isNotEmpty(equipJson)) {
            roleEquip = JsonUtils.string2Object(equipJson, RoleEquip.class);
        }

    }

    /**
     * save hook
     */
    public void doBeforeSave() {
        if (pet != null) {
            petJson = JsonUtils.object2String(pet);
        }
        if (roleEquip != null) {
            equipJson = JsonUtils.object2String(roleEquip);
        }
    }
}
